package cdc.cs.service;

import cdc.cs.common.HttpWithAuthService;
import cdc.cs.config.ODataConfig;
import cdc.cs.entity.MobilePhoneNumberVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;

/**
 * @ClassName: public class ODataPartnersRoleService {
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/28 15:28
 * @Version: 1.0
 */
@Service
@Slf4j
public class ODataPartnersPhoneService {

    private MobilePhoneNumberVO mobilePhoneNumberVO;

    /**
     * 获取单个手机号
     *
     * @param url
     * @return
     */
    public String getOnePhone(String url) {
        String result = null;
        int count = 2;
        while (count > 0) {
            try {
                result = HttpWithAuthService.getService(url);
            } catch (IOException e) {
                log.error("获取Partner总数网络异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (StringUtils.isEmpty(result)) {
                count--;
            } else {
                break;
            }

        }
        return result;
    }

    /**
     * 解析手机号
     *
     * @param response
     */
    private MobilePhoneNumberVO parsePhoneJson(String response) {
        MobilePhoneNumberVO numberVO = new MobilePhoneNumberVO();
        JSONObject obj = (JSONObject) JSONObject.parse(response);
        JSONObject d = (JSONObject) obj.get("d");
        JSONArray results = d.getJSONArray("results");
        if (results.size() > 0) {
            for (int i = 0; i < results.size(); i++) {
                JSONObject object = results.getJSONObject(i);
                numberVO.setAddressID(object.getString("AddressID"));
                numberVO.setPhoneNumber(object.getString("PhoneNumber"));
                numberVO.setInternationalPhoneNumber(object.getString("InternationalPhoneNumber"));
            }
            this.mobilePhoneNumberVO = numberVO;
            return numberVO;
        } else {
            return null;
        }

    }

    /**
     * 数据转换
     * @param phoneNumberVO
     * @return
     */
    public JSONObject dataChange(MobilePhoneNumberVO phoneNumberVO, String businessPartner) {
        JSONObject result = new JSONObject();
        result.put("BusinessPartner", businessPartner);
        result.put("AddressID", phoneNumberVO.getAddressID());
        result.put("InternationalPhoneNumber", phoneNumberVO.getInternationalPhoneNumber());
        result.put("PhoneNumber", phoneNumberVO.getPhoneNumber());
        return result;
    }

    public boolean savePartnerPhone(String url, String businessPartner) {
        String onePhone = getOnePhone(url);
        MobilePhoneNumberVO numberVO = parsePhoneJson(onePhone);
        if (numberVO != null) {
            boolean result = savePartnerPhone(numberVO, businessPartner);
            if (!result) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    public boolean savePartnerPhone(MobilePhoneNumberVO phoneNumberVO, String businessPartner) {
        JSONObject jsonObject = dataChange(phoneNumberVO, businessPartner);
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.postService(ODataConfig.targetPartnerPhone, jsonObject);
                flag = true;
            } catch (IOException e) {
                log.error("保存PartnerPhone出现异常，未保存数据为:{}，地址为:{}", jsonObject, ODataConfig.targetPartnerPhone);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
        return flag;
    }

    /**
     * 删除手机号码
     * @param url
     */
    public void deletePartnerPhone(String url) {
        int count = 2;
        boolean flag = false;
        while (count > 0) {
            try {
                HttpWithAuthService.deleteService(url);
                flag = true;
            } catch (IOException e) {
                log.error("删除TargetPartnerPhone异常，异常信息为:{}，地址为:{}", e.getMessage(), url);
            }
            if (flag) {
                break;
            } else {
                count--;
            }
        }
    }
}
